﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WROA.Models.Entitys;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using WROA.Models.IServices;
using System.Web.Mvc;

namespace WROA.Models.Services
{
    public class UserService : Services, IUserService
    {
        /// <summary>
        /// 判断登陆是否通过
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool IsPass(string name, string password)
        {
            bool result;
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("U_password", password, BinaryOperatorType.Equal));
            XPCollection xp = new XPCollection(typeof(Users), criteria);
            if (xp.Count == 1)//登陆通过
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据用户名查询用户角色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string SelectRole(string name)
        {
            string role = "";
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                role = u.Role.Ro_name;
            }
            return role;
        }

        /// <summary>
        /// 根据用户名返回用户角色对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Role GetRole(string name)
        {
            Role role = null;
            XPCollection xp = new XPCollection(typeof(Role), new BinaryOperator("Ro_name", name, BinaryOperatorType.Equal));
            foreach (Role r in xp)
            {
                role = r;
            }
            return role;
        }

        /// <summary>
        /// 根据用户名查询用户职位
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string SelectPosition(string name)
        {
            string position = "";
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                position = u.Position.Po_name;
            }
            return position;
        }


        /// <summary>
        /// 根据用户名返回用户职位对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Position GetPosition(string name)
        {
            Position position = null;
            XPCollection xp = new XPCollection(typeof(Position), new BinaryOperator("Po_name", name, BinaryOperatorType.Equal));
            foreach (Position p in xp)
            {
                position = p;
            }
            return position;
        }

        /// <summary>
        /// 根据用户名修改用户角色
        /// </summary>
        /// <param name="name"></param>
        /// <param name="role"></param>
        public void UpdateRole(string name, string role)
        {
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                u.Role.Ro_name = role;
                u.Save();
            }
        }

        /// <summary>
        /// 根据用户名修改用户职位
        /// </summary>
        /// <param name="name"></param>
        /// <param name="position"></param>
        public void UpdatePosition(string name, string position)
        {
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                u.Role.Ro_name = position;
                u.Save();
            }
        }

        /// <summary>
        /// 根据用户名修改用户密码
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        public void UpdatePassword(string name, string password)
        {
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                u.U_password = password;
                u.Save();
            }
        }

        /// <summary>
        /// 修改用户个人资料
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUserData(Users user)
        {
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", user.U_name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                u.U_realname = user.U_realname;
                u.U_gender = user.U_gender;
                u.U_email = user.U_email;
                u.U_phone = user.U_phone;
                u.U_mobile = user.U_mobile;
                u.U_birth = user.U_birth;
                u.Save();
            }
        }

        /// <summary>
        /// 判断用户名是否已经存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsExist(string name)
        {
            bool result;
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            if (xp.Count == 1)//用户名存在
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
        
        /// <summary>
        /// 根据用户名返回一个用户对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Users GetUser(string name)
        {
            Users user = null;
            XPCollection xp = new XPCollection(typeof(Users), new BinaryOperator("U_name", name, BinaryOperatorType.Equal));
            foreach (Users u in xp)
            {
                user = u;
            }
            return user;
        }

        /// <summary>
        /// 返回所有用户资料
        /// </summary>
        /// <returns></returns>
        public XPCollection GetAllUser()
        {
            XPCollection xp = new XPCollection(typeof(Users));
            SortProperty sp = new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 获取角色名列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetRoleListName()
        {
            IList<SelectListItem> result = new List<SelectListItem>();
            foreach (Role r in new XPCollection(typeof(Role)))
            {
                result.Add(new SelectListItem() { Text = r.Ro_name, Value = r.Ro_name });
            }
            return result;
        }

        /// <summary>
        /// 获取职位名列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetPositionListName()
        {
            IList<SelectListItem> result = new List<SelectListItem>();
            foreach (Position p in new XPCollection(typeof(Role)))
            {
                result.Add(new SelectListItem() { Text = p.Po_name, Value = p.Po_name });
            }
            return result;
        }
    }
}